﻿namespace Numbrella.Client.Common
{
	using System;
	using System.Diagnostics;
	using System.ServiceModel;
	using System.Threading;
	using Numbrella.Interaction;

	public static class CommonFunctions
	{
		public static void Timeout(double seconds)
		{
			Trace.TraceInformation("Waiting for {0} seconds...", seconds);
			Thread.Sleep(TimeSpan.FromSeconds(seconds));
		}

		public static TrackerProxy OpenConnectionToTracker(string connectionString, string userName, string password)
		{
			EndpointAddress remoteAddress = new EndpointAddress(new Uri(connectionString), EndpointIdentity.CreateDnsIdentity("tempCert"));
            TrackerProxy proxy = new TrackerProxy("TrackerService", remoteAddress);
			proxy.ClientCredentials.UserName.UserName = userName;
			proxy.ClientCredentials.UserName.Password = password;

			return proxy;
		}

		public static string UnwrapExceptionMessage(Exception exception)
		{
			string result = String.Empty;
			while (exception != null)
			{
				result += exception.Message;
				exception = exception.InnerException;
				if (exception != null)
					result += " ";
			}

			return result;
		}
	}
}